﻿using HuiLian.Data.DataAccess.Base;
using HuiLian.Data.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace HuiLian.Data.DataAccess.Implement
{
    public class BaseDaoImpl<Entity> : IBaseDao<Entity> where Entity : class, new()
    {
        public readonly EFContext Context;

        public BaseDaoImpl()
        {
            this.Context = EFContext.Context;
        }

        public BaseDaoImpl(EFContext Context)
        {
            this.Context = Context;
        }

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> Insert(params Entity[] entities)
        {
            foreach (Entity entity in entities)
            {
                Context.Entry(entity).State = EntityState.Added;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> Delete(params Entity[] entities)
        {
            foreach (Entity entity in entities)
            {
                Context.Entry(entity).State = EntityState.Deleted;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> Update(params Entity[] entities)
        {
            foreach (Entity entity in entities)
            {
                Context.Entry(entity).State = EntityState.Modified;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<Entity> Select(Expression<Func<Entity, bool>> where)
        {
            return await Context.Set<Entity>().Where(where).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<Entity> GetList(Expression<Func<Entity, bool>> where)
        {
            return Context.Set<Entity>().Where(where);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<Entity> GetList()
        {
            return GetList(entity => true);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="Key"></typeparam>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public IQueryable<Entity> PageQuery<Key>(Expression<Func<Entity, bool>> where,
            Expression<Func<Entity, Key>> orderby, int pageSize, int pageIndex, bool isAsc = true)
        {
            IQueryable<Entity> entities = Context.Set<Entity>().Where(where);
            entities = isAsc ? entities.OrderBy(orderby) : entities.OrderByDescending(orderby);
            return entities.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }

    }
}
